package com.mallcloud.mall.admin.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mallcloud.mall.admin.api.dto.UserDTO;
import com.mallcloud.mall.admin.api.dto.UserInfo;
import com.mallcloud.mall.admin.api.dto.UserPasswordDTO;
import com.mallcloud.mall.admin.api.dto.UserProfileDTO;
import com.mallcloud.mall.admin.api.entity.SysUser;
import com.mallcloud.mall.admin.api.vo.UserExcelVO;
import com.mallcloud.mall.admin.api.vo.UserVO;
import com.mallcloud.mall.admin.service.SysUserService;
import com.mallcloud.mall.common.core.exception.ErrorCodes;
import com.mallcloud.mall.common.core.util.MsgUtils;
import com.mallcloud.mall.common.core.util.R;
import com.mallcloud.mall.common.excel.annotation.RequestExcel;
import com.mallcloud.mall.common.excel.annotation.ResponseExcel;
import com.mallcloud.mall.common.security.annotation.Inner;
import com.mallcloud.mall.common.security.util.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@Api(tags = "用户管理模块")
@RestController
@AllArgsConstructor
@RequestMapping("/user")
public class SysUserController {
	private final SysUserService userService;

	@ApiOperation("获取指定用户全部信息")
	@Inner
	@GetMapping("/info/{username}")
	public R<UserInfo> info(@PathVariable String username) {
		SysUser user = userService.getOne(Wrappers.<SysUser>query().lambda().eq(SysUser::getUsername, username));
		if (user == null) {
			return R.failed(MsgUtils.getMessage(ErrorCodes.SYS_USER_USERINFO_EMPTY, username));
		}
		return R.ok(userService.findUserInfo(user));
	}

	@ApiOperation("获取当前用户全部信息")
	@GetMapping(value = { "/info" })
	public R<UserInfo> info() {
		String username = SecurityUtils.getUser().getUsername();
		SysUser user = userService.getOne(Wrappers.<SysUser>query().lambda().eq(SysUser::getUsername, username));
		if (user == null) {
			return R.failed(MsgUtils.getMessage(ErrorCodes.SYS_USER_QUERY_ERROR));
		}
		return R.ok(userService.findUserInfo(user));
	}

	@ApiOperation("通过ID查询用户信息")
	@GetMapping("/{id}")
	public R<UserVO> user(@PathVariable Long id) {
		return R.ok(userService.selectUserVoById(id));
	}

	@ApiOperation("根据用户名查询用户信息")
	@GetMapping("/details/{username}")
	public R<SysUser> user(@PathVariable String username) {
		SysUser condition = new SysUser();
		condition.setUsername(username);
		return R.ok(userService.getOne(new QueryWrapper<>(condition)));
	}

	@ApiOperation("删除用户信息")
	@DeleteMapping("/{id}")
	@PreAuthorize("@pms.hasPermission('sys_user_del')")
	@ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "int", paramType = "path")
	public R<Boolean> userDel(@PathVariable Long id) {
		SysUser sysUser = userService.getById(id);
		return R.ok(userService.deleteUserById(sysUser));
	}

	@ApiOperation("添加用户")
	@PostMapping
	@PreAuthorize("@pms.hasPermission('sys_user_add')")
	public R<Boolean> user(@RequestBody UserDTO userDto) {
		return R.ok(userService.saveUser(userDto));
	}

	@ApiOperation("更新用户信息")
	@PutMapping
	@PreAuthorize("@pms.hasPermission('sys_user_edit')")
	public R<Boolean> updateUser(@Valid @RequestBody UserDTO userDto) {
		return R.ok(userService.updateUser(userDto));
	}

	@ApiOperation("分页查询用户")
	@GetMapping("/page")
	public R<IPage<UserVO>> getUserPage(Page<UserVO> page, UserDTO userDTO) {
		return R.ok(userService.getUsersWithRolePage(page, userDTO));
	}

	@ApiOperation("修改个人信息")
	@PutMapping("/edit")
	public R<Boolean> updateUserInfo(@Valid @RequestBody UserDTO userDto) {
		return R.ok(userService.updateUserInfo(userDto));
	}

	@ApiOperation("修改个人信息")
	@PutMapping("/update-profile")
	public R<Boolean> updateProfile(@Valid @RequestBody UserProfileDTO userDto) {
		return R.ok(userService.updateProfile(userDto));
	}

	@ApiOperation("修改密码")
	@PutMapping("/update-password")
	public R<Boolean> updatePassword(@Valid @RequestBody UserPasswordDTO userDto) {
		return R.ok(userService.updatePassword(userDto));
	}

	@ApiOperation("上级部门用户列表")
	@GetMapping("/ancestor/{username}")
	public R<List<SysUser>> listAncestorUsers(@PathVariable String username) {
		return R.ok(userService.listAncestorUsers(username));
	}

	@ApiOperation("导出excel 表格")
	@ResponseExcel
	@GetMapping("/export")
	@PreAuthorize("@pms.hasPermission('sys_user_export')")
	public List<UserExcelVO> export(UserDTO userDTO) {
		return userService.listUser(userDTO);
	}

	/**
	 * 导入用户
	 * @param excelVOList 用户列表
	 * @param bindingResult 错误信息列表
	 */
	@ApiOperation("导入用户")
	@PostMapping("/import")
	@PreAuthorize("@pms.hasPermission('sys_user_export')")
	public R importUser(@RequestExcel List<UserExcelVO> excelVOList, BindingResult bindingResult) {
		return userService.importUser(excelVOList, bindingResult);
	}

	@ApiOperation("锁定指定用户")
	@Inner
	@PutMapping("/lock/{username}")
	public R<Boolean> lockUser(@PathVariable String username) {
		return userService.lockUser(username);
	}



}
